<script setup lang="ts">
import type { Logistics } from '@/types/order'
import { ref, onMounted } from 'vue'
import { getOrderLogistics } from '@/api/order'
import { useRoute } from 'vue-router'
import AMapLoader from '@amap/amap-jsapi-loader'
import startImg from '@/assets/start.png'
import endImg from '@/assets/end.png'
import carImg from '@/assets/car.png'
window._AMapSecurityConfig = {
  securityJsCode: '99240abf1188823cd88cfe635a521af3'
}
const route = useRoute()
const logistics = ref<Logistics>()
onMounted(async () => {
  const res = await getOrderLogistics(route.params.id as string)
  logistics.value = res.data

  // 数据有了之后，这个时候id=map的div也准备好了
  AMapLoader.load({
    key: 'b5a3290bb8e23f2361bcc36be9435bcd', // 申请好的Web端开发者Key，首次调用 load 时必填
    version: '2.0', // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
    plugins: [''] // 需要使用的的插件列表，如比例尺'AMap.Scale'等
  }).then(AMap => {
    const map = new AMap.Map('map', {
      //设置地图容器id
      mapStyle: 'amap://styles/whitesmoke',
      zoom: 12 //初始化地图级别
    })

    AMap.plugin('AMap.Driving', function () {
      var driving = new AMap.Driving({
        map: map, // 把路线花在地图上
        showTraffic: false, // 关闭交通状态
        hideMarkers: true
      })

      // 起点
      const startPoint = res.data.logisticsInfo.shift()
      var startMarker = new AMap.Marker({
        icon: startImg,
        position: [startPoint?.longitude, startPoint?.latitude],
        anchor: 'center'
      })
      // 把标注（覆盖物）添加到地图上
      map.add(startMarker)

      // 终点
      const endPoint = res.data.logisticsInfo.pop()
      var endMarker = new AMap.Marker({
        icon: endImg,
        position: [endPoint?.longitude, endPoint?.latitude],
        anchor: 'center'
      })
      // 把标注（覆盖物）添加到地图上
      map.add(endMarker)

      /**
       * 参数1：起点
       * 参数2：终点
       * 参数3：途径点，它里面是一个数组，最多支持16个途径点
       */
      driving.search(
        [startPoint?.longitude, startPoint?.latitude],
        [endPoint?.longitude, endPoint?.latitude],
        {
          waypoints: res.data.logisticsInfo.map(item => {
            return [item.longitude, item.latitude]
          })
        },
        function (status: any, result: any) {
          // 未出错时，result即是对应的路线规划方案
          // 把当前车车这个覆盖物，绘制在地图上
          const currentPoint = [
            res.data.currentLocationInfo.longitude,
            res.data.currentLocationInfo.latitude
          ]
          var carMarker = new AMap.Marker({
            icon: carImg,
            position: currentPoint,
            anchor: 'center'
          })
          // 把标注（覆盖物）添加到地图上
          map.add(carMarker)

          setTimeout(() => {
            map.setFitView([carMarker])
            map.setZoom(9)
          }, 3000)
        }
      )
    })
  })
})
</script>

<template>
  <div class="order-logistics-page">
    <div id="map">
      <div class="title">
        <van-icon name="arrow-left" @click="$router.back()" />
        <span>{{ logistics?.statusValue }}</span>
        <van-icon name="service" />
      </div>
      <div class="current">
        <p class="status">订单派送中 预计{{ logistics?.estimatedTime }}送达</p>
        <p class="predict">
          <span>{{ logistics?.name }}</span>
          <span>{{ logistics?.awbNo }}</span>
        </p>
      </div>
    </div>
    <div class="logistics">
      <p class="title">物流详情</p>
      <van-steps direction="vertical" :active="0">
        <van-step v-for="item in logistics?.list" :key="item.id">
          <p class="status">{{ item.statusValue }}</p>
          <p class="content">
            {{ item.content }}
          </p>
          <p class="time">{{ item.createTime }}</p>
        </van-step>
      </van-steps>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.order-logistics-page {
  --van-step-icon-size: 18px;
  --van-step-circle-size: 10px;
}
#map {
  height: 450px;
  background-color: var(--cp-bg);
  overflow: hidden;
  position: relative;
  .title {
    background-color: #fff;
    height: 46px;
    width: 355px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    padding: 0 15px;
    font-size: 16px;
    position: absolute;
    left: 10px;
    top: 10px;
    box-sizing: border-box;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    z-index: 999;
    > span {
      flex: 1;
      text-align: center;
    }
    .van-icon {
      font-size: 18px;
      color: #666;
      &:last-child {
        color: var(--cp-primary);
      }
    }
  }
  .current {
    height: 80px;
    border-radius: 4px;
    background-color: #fff;
    height: 80px;
    width: 355px;
    box-sizing: border-box;
    padding: 15px;
    position: absolute;
    left: 10px;
    bottom: 10px;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    z-index: 999;
    .status {
      font-size: 15px;
      line-height: 26px;
    }
    .predict {
      color: var(--cp-tip);
      font-size: 13px;
      margin-top: 5px;
      > span {
        padding-right: 10px;
      }
    }
  }
}
.logistics {
  padding: 0 10px 20px;
  .title {
    font-size: 16px;
    padding: 15px 5px 5px;
  }
  .van-steps {
    :deep(.van-step) {
      &::after {
        display: none;
      }
    }
    .status {
      font-size: 15px;
      color: var(--cp-text3);
      margin-bottom: 4px;
    }
    .content {
      font-size: 13px;
      color: var(--cp-tip);
      margin-bottom: 4px;
    }
    .time {
      font-size: 13px;
      color: var(--cp-tag);
    }
  }
}
</style>
